//% color="#1E90FF" iconWidth=50 iconHeight=40
namespace deepseek {

    //% block="初始化deepseek API密钥:[API] 模型:[MODEL] 温度:[TEMPERATURE]" blockType="command"
    //% API.shadow="string" API.defl="Bearer YOUR_API"
    //% MODEL.shadow="dropdown" MODEL.options="MODEL"

    //% TEMPERATURE.shadow="dropdown" TEMPERATURE.options="temperature"

    export function initializedeepseekAI(parameter: any, block: any) {
	let kimi_api = parameter.API.code;
	let model = parameter.MODEL.code;
	let temperature = parameter.TEMPERATURE.code;
	Generator.addInclude('HTTPClient', '#include <HTTPClient.h>');
	Generator.addInclude('ArduinoJson', '#include <ArduinoJson.h>');
	Generator.addObject(`HTTPClient`, `HTTPClient`, `http;`);
	Generator.addObject(`DynamicJsonBuffer`, `DynamicJsonBuffer`, `jsonBuffer;`);
	Generator.addSetup(`begin`, `http.begin("https://api.siliconflow.cn/v1/chat/completions");`);
	Generator.addSetup(`Content-Type`, `http.addHeader("Content-Type", "application/json");`);
	Generator.addSetup(`Authorization`, `http.addHeader("Authorization", ${kimi_api});`);
	Generator.addObject(`model`, `String`,`model="${model}";`);
	Generator.addObject(`temperature`, `float`,`temperature=${temperature};`);
	Generator.addObject(`chatmessages`, `String`,`deepseek_system;`);
	Generator.addObject(`messageCount`, `int`,`messageCount = 1;`);
	Generator.addObject(`maxMessages`, `int`,`maxMessages = 10;`);
	 }
    //% block="初始化Deepseek模型角色:[deepseek_system] " blockType="command"
    //% deepseek_system.shadow="string" deepseek_system.defl="你是deepseek，你更擅长中文对话。"
    
    export function initializedeepseekSystem(parameter: any, block: any) {
	let deepseek_system = parameter.deepseek_system.code;
	Generator.addSetup(`deepseek_system`, `deepseek_system =${deepseek_system};`);
 }
 //% block="发送消息 [MESSAGE]轮数:[maxMessages]字数:[chatsize]" blockType="command"
 //% MESSAGE.shadow="string" MESSAGE.defl="你好，AI"
 //% maxMessages.shadow="range"   maxMessages.params.min=1    maxMessages.params.max=10    maxMessages.defl=5
//% chatsize.shadow="range"   chatsize.params.min=50    chatsize.params.max=300    chatsize.defl=150

    export function sendMessage(parameter: any, block: any) {
        let chatsize = parameter.chatsize.code;
	let userMessage = parameter.MESSAGE.code;
	let maxMessages = parameter.maxMessages.code;
  	Generator.addCode(`	maxMessages=${maxMessages};`);
	Generator.addCode(`
		String mydata = String("{\\"model\\":\\"")+model+String("\\",\\"max_tokens\\":4090,\\"temperature\\":")+String(temperature)+String(",\\"messages\\":[");

		mydata += String("{\\"role\\":\\"system\\",\\"content\\":\\"")+String(deepseek_system)+String("，回答字数为${chatsize}")+String("字\\"},");
		mydata += String("{\\"role\\":\\"user\\",\\"content\\":\\"") + String(${userMessage}) + String("\\"}");
		mydata += String("]}");
		http.POST(mydata);
		String jsonString=http.getString();
		while(jsonString==""){
		  http.POST(mydata);
		  jsonString=http.getString();
		  delay(100);
		}
		JsonObject& root = jsonBuffer.parseObject(jsonString);
		String text = root["choices"][0]["message"]["content"];
	`);

  }
   
    //% block="返回结果" blockType="reporter"
    export function reMessage(parameter: any, block: any) {
	Generator.addCode(`text`);
	}
 }
